const app = getApp()
import util from '../../../utils/util'
import {
  ServiceName,
  ServiceList
} from '../../../config/api'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    limit: 10,
    cur_index: 99, //当前激活选中项，默认为0
    catpid: '',
    columns: [], //栏目数组
    service_list: [],
    isBottom: false,
    isEmpty: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getServiceCatetory();
  },

  getServiceCatetory() {
    let that = this;
    util.apiRequest(ServiceName, {
      vollege_id: app.globalData.vollege_id
    }, null).then(r => {
      console.log('服务项目：', r)
      that.setData({
        columns: r.data.data,
        //catpid: r.data.data[0].id
      })
      this.getServiceList() // 调用列表
    })
  },

  getServiceList() {
    console.log('catpid', this.data.catpid)
    let that = this;
    let params = {
      catpid: this.data.catpid,
      page: this.data.page,
      limit: this.data.limit,
      vollege_id: app.globalData.vollege_id
    }

    wx.showLoading({
      title: '玩命加载中',
    })

    util.apiRequest(ServiceList, params, null).then(r => {
      console.log('服务列表', r)

      var newList = that.data.service_list.concat(r.data.data.data)

      if (r.data.data.data == '') {
        that.setData({
          service_list: newList,
          isBottom: true
        })
        wx.showToast({
          title: '暂无更多',
          icon: 'none',
        })
      } else {
        that.setData({
          service_list: newList
        })
      }
      wx.hideLoading();

      if (newList.length == 0) {
        that.setData({
          isEmpty: true
        })
      } else {
        that.setData({
          isEmpty: false
        })
      }

    })
  },

  SwitchStatus: function(e) {
    
    let id = e.currentTarget.dataset.id;
    let cur_index = e.currentTarget.dataset.index;

    console.log('当前项index',cur_index)

    let that = this;
    that.setData({
      cur_index,
      catpid: id,
      page: 1,
      service_list: [],
    });
    this.getServiceList()
  },

  toDetail: function(e) {
    console.log('eee', e)
    let id = e.currentTarget.dataset.id;
    let status = e.currentTarget.dataset.status;
    if (status == 1 || status == 4) {
      wx.navigateTo({
        url: '/pages/culture/unsolved/unsolved?id=' + id,
      })
    } else {
      wx.navigateTo({
        url: '/pages/culture/resolved/resolved?id=' + id,
      })
    }
  },

  toHelp: function() {
    wx.navigateTo({
      url: '/pages/culture/seekHelp/seekHelp',
    })
  },


  onReachBottom: function() {
    var that = this;
    var page = this.data.page;
    page++;
    that.setData({
      page: page
    })
    console.log('page++', page)
    this.getServiceList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})